/*
 * Created on Jul 14, 2005
 *
 */
package com.erclab.internal.xpresso.unittests;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import junit.framework.TestCase;

import com.erclab.internal.xpresso.*;
import com.erclab.internal.xpresso.reports.*;
import com.erclab.internal.xpresso.security.User;
import com.erclab.internal.xpresso.conf.Settings;
import com.erclab.internal.xpresso.exceptions.ApplicationException;
import com.erclab.internal.xpresso.exceptions.DirectoryException;

/**
 * @author Enrique Rodriguez.
 *
 */
public class ReportFilterTest extends TestCase {

	Application xpresso = null;
	User user = null;

	/**
	 * Constructor for ReportFilterTest.
	 * @param arg0
	 */
	public ReportFilterTest(String arg0) {
		super(arg0);
	}
	
	protected void setUp() throws Exception {
		super.setUp();
		try {
			xpresso = new Application();
		} catch(ApplicationException e){
			fail("Create Application failed: " + e.toString());
		}
		try {
			user = new User(); 
			Settings.getUserDirectory().loadUserBean(user);
		} catch (DirectoryException e) {
			fail();
			e.printStackTrace();
		}
	}
	
	public void testDisplayFilters(){
		System.out.println("\n\nTest display filters ---------------------");
		
		ReportParameters parameters = new ReportParameters();
		parameters.setPageNum(1);
		parameters.setUniqueKeyValue("1");
		parameters.setUser(user);
		
		Report theReport = null;
		try {
			theReport = ReportFactory.loadReport("UnitTestReports");
		} catch (Exception e1) {
			e1.printStackTrace();
			fail();
		}
		
		ArrayList filters = theReport.getReportFilters();
		for (int i =0; i < filters.size(); i++){
			ReportFilter f = (ReportFilter)filters.get(i);
			System.out.println(f.getLabel() + " --> " + f.conditionTypesToHTML(theReport) 
								+ " - " + f.filterBoxToHTML(theReport));
		}
		
	}
	
	public void testCreateReportWithFilters(){
		
		HashMap conditions = new HashMap();
		conditions.put("FilterReportName", new String[]{"AllReports.config"});
		conditions.put("FilterReportNameCondition", new String[]{"Equals"});
		runReport(conditions);

		conditions = new HashMap();
		conditions.put("FilterReportName", new String[]{"lReports.con"});
		conditions.put("FilterReportNameCondition", new String[]{"Contains"});
		runReport(conditions);

		conditions = new HashMap();
		conditions.put("FilterReportName", new String[]{"AllRep"});
		conditions.put("FilterReportNameCondition", new String[]{"Starts With"});
		runReport(conditions);
		
		conditions = new HashMap();
		conditions.put("FilterReportName", new String[]{"ts.config"});
		conditions.put("FilterReportNameCondition", new String[]{"Ends With"});
		runReport(conditions);
		
		conditions = new HashMap();
		conditions.put("FilterReportName", new String[]{"a"});
		conditions.put("FilterReportNameCondition", new String[]{"Contains"});
		conditions.put("FilteridDataSource", new String[]{"1/1/2005"});
		conditions.put("FilteridDataSource2", new String[]{"2/2/2005"});
		conditions.put("FilteridDataSourceCondition", new String[]{"After"});
		runReport(conditions);
		
	}

	private void runReport(HashMap conditions) {
		System.out.println("\n\ntestCreateReportWithFilters() ---------------------");
		
		ReportParameters parameters = new ReportParameters();
		parameters.setPageNum(1);
		parameters.setUniqueKeyValue("1");
		parameters.setUser(user);
		
		// Put the conditions
		parameters.setConditions(conditions);
		
				
		Report theReport = null;
		try {
			theReport = ReportFactory.loadReport("UnitTestReports");
		} catch (Exception e1) {
			e1.printStackTrace();
			fail();
		}
		System.out.println(theReport);
		
		// Load the parameters of the report
		theReport.setReportParameters(parameters);
		
		ReportResultSet rrs = null;
		try {
			rrs = ReportFactory.executeReportQuery(theReport);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
		
		// Print result set:
		assertTrue(rrs != null);
		for (int i =0; i < rrs.getNumberOfColumns(); i++){
			System.out.print(rrs.getHeaderText(i) + " | ");
		}
		while(rrs.nextRow()){
			System.out.println("");
			for (int i =0; i < rrs.getNumberOfColumns(); i++){
				System.out.print(rrs.getRowValue(i) + " | ");
			}			
		}
	}


}
